<template>
  <div class="shopping-cart">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="header-left">
        <div class="back-arrow" @click="goBack">
          <span>‹</span>
        </div>
        <h1 class="title">购物车</h1>
      </div>
      <div class="header-right">
        <div class="cart-badge">
          <span class="badge-number">{{ cartItems.length }}</span>
        </div>
      </div>
    </div>

    <!-- 收货地址 -->
    <div class="address-section">
      <h3 class="section-title">收货地址</h3>
      <div class="address-content">
        <div class="address-info">
          <span class="location-icon">📍</span>
          <span class="address-text">{{ selectedAddress || '北京市海淀区中关村某某大厦单元-楼层号' }}</span>
        </div>
        <div class="change-button" @click="showMap = true">
          <span>更改</span>
        </div>
      </div>
    </div>

    <!-- 地图选择组件 -->
    <AddressMap
      v-model:visible="showMap"
      @select-address="handleAddressSelect"
    />

    <!-- 商品列表 -->
    <div class="products-section">
      <div v-if="cartItems.length === 0" class="empty-cart">
        <div class="empty-icon">🛒</div>
        <p class="empty-text">购物车是空的</p>
        <p class="empty-subtext">快去添加一些商品吧</p>
      </div>
      <div class="product-item" v-for="item in cartItems" :key="item.id" v-else>
        <div class="product-image">
          <img :src="item.image" :alt="item.name" />
        </div>
        <div class="product-info">
          <h4 class="product-name">{{ item.name }}</h4>
          <p class="product-spec">{{ item.specification }}</p>
          <div class="price-quantity">
            <span class="price">¥{{ item.price }}</span>
                         <div class="quantity-control">
               <button class="quantity-btn minus" @click="decreaseQuantity(item.id)">-</button>
               <span class="quantity">{{ item.quantity.toString().padStart(2, '0') }}</span>
               <button class="quantity-btn plus" @click="increaseQuantity(item.id)">+</button>
             </div>
          </div>
        </div>
        <div class="delete-button" @click="removeItem(item.id)">
          <span class="delete-icon">🗑️</span>
        </div>
      </div>
    </div>

    <!-- 医疗实验室凭证 -->
    <div class="certificate-section">
      <div class="certificate-content">
        <span class="certificate-icon">🏥</span>
        <span class="certificate-text">医疗实验室凭证</span>
        <span class="certificate-name">MediaCal</span>
      </div>
    </div>

    <!-- 底部结算 -->
    <div class="bottom-section" v-if="cartItems.length > 0">
      <div class="total-section">
        <span class="total-label">总计</span>
        <span class="total-price">¥{{ totalPrice }}</span>
      </div>
      <button class="submit-order-btn" @click="submitOrder">
        提交订单
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import AddressMap from '../../components/AddressMap.vue'

const router = useRouter()
const showMap = ref(false)
const selectedAddress = ref('')

// 购物车商品数据
const cartItems = ref<any[]>([])

// 从 localStorage 读取购物车数据
const loadCartItems = () => {
  const savedCartItems = localStorage.getItem('cartItems')
  if (savedCartItems) {
    cartItems.value = JSON.parse(savedCartItems)
  } else {
    // 如果没有保存的数据，使用默认数据
    cartItems.value = [
      {
        id: 1,
        name: '碳酸钙D3维钙营养行 健六顺·朗迪',
        specification: '片剂 60片',
        price: '64.00',
        quantity: 2,
        image: '/src/views/sow/imgs/v2_sypcwt.svg'
      },
      {
        id: 2,
        name: '碳酸钙D3维钙营养行 健六顺·朗迪',
        specification: '片剂 60片',
        price: '32.00',
        quantity: 2,
        image: '/src/views/sow/imgs/v2_sypcz0.svg'
      },
      {
        id: 3,
        name: '碳酸钙D3维钙营养行 健六顺·朗迪',
        specification: '片剂 60片',
        price: '97.00',
        quantity: 2,
        image: '/src/views/sow/imgs/v2_sypcxi.svg'
      }
    ]
  }
}

// 计算总价
const totalPrice = computed(() => {
  return cartItems.value.reduce((total, item) => {
    return total + (parseFloat(item.price) * item.quantity)
  }, 0).toFixed(2)
})

// 方法
const goBack = () => {
  router.back()
}

const increaseQuantity = (itemId: number) => {
  const item = cartItems.value.find(item => item.id === itemId)
  if (item) {
    item.quantity++
    // 保存更新后的购物车数据
    localStorage.setItem('cartItems', JSON.stringify(cartItems.value))
  }
}

const decreaseQuantity = (itemId: number) => {
  const item = cartItems.value.find(item => item.id === itemId)
  if (item && item.quantity > 1) {
    item.quantity--
    // 保存更新后的购物车数据
    localStorage.setItem('cartItems', JSON.stringify(cartItems.value))
  }
}

const removeItem = (itemId: number) => {
  const index = cartItems.value.findIndex(item => item.id === itemId)
  if (index > -1) {
    cartItems.value.splice(index, 1)
    // 保存更新后的购物车数据
    localStorage.setItem('cartItems', JSON.stringify(cartItems.value))
  }
}

const submitOrder = () => {
  console.log('提交订单')
  // 跳转到确认订单页面
  router.push('/confirm-order')
}

// 处理地址选择
const handleAddressSelect = (address: any) => {
  console.log('收到选择的地址:', address) // 添加日志
  selectedAddress.value = address.address
  localStorage.setItem('deliveryAddress', JSON.stringify(address))
}

// 组件加载时读取保存的地址
onMounted(() => {
  const savedAddress = localStorage.getItem('deliveryAddress')
  if (savedAddress) {
    const address = JSON.parse(savedAddress)
    selectedAddress.value = address.address
  }
})

// 页面加载时读取购物车数据
onMounted(() => {
  loadCartItems()
})
</script>

<style scoped>
.shopping-cart {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: white;
  min-height: 100vh;
  padding-bottom: 100px;
}

/* 顶部导航栏 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.back-arrow {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 24px;
  color: #333;
  transition: color 0.2s ease;
}

.back-arrow:hover {
  color: #4A90E2;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
}

.cart-badge {
  width: 20px;
  height: 20px;
  background: #4A90E2;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.badge-number {
  color: white;
  font-size: 10px;
  font-weight: 600;
}

/* 收货地址 */
.address-section {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 12px 0;
}

.address-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.address-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.location-icon {
  font-size: 16px;
  color: #666;
}

.address-text {
  font-size: 14px;
  color: #333;
  line-height: 1.4;
}

.change-button {
  color: #4A90E2;
  font-size: 14px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.2s ease;
}

.change-button:hover {
  background: rgba(74, 144, 226, 0.1);
}

/* 商品列表 */
.products-section {
  padding: 0 20px;
}

.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.6;
}

.empty-text {
  font-size: 16px;
  color: #666;
  margin: 0 0 8px 0;
}

.empty-subtext {
  font-size: 14px;
  color: #999;
  margin: 0;
}

.product-item {
  display: flex;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid #f0f0f0;
  gap: 12px;
}

.product-item:last-child {
  border-bottom: none;
}

.product-image {
  width: 60px;
  height: 60px;
  flex-shrink: 0;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 8px;
  background: #f8f9fa;
  padding: 4px;
}

.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.product-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin: 0;
  line-height: 1.3;
}

.product-spec {
  font-size: 12px;
  color: #666;
  margin: 0;
}

.price-quantity {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
}

.price {
  font-size: 16px;
  font-weight: 600;
  color: #4CAF50;
}

.quantity-control {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #f8f9fa;
  border-radius: 16px;
  padding: 2px 6px;
}

.quantity-btn {
  width: 20px;
  height: 20px;
  border: none;
  background: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 14px;
  color: #666;
  transition: all 0.2s ease;
}

.quantity-btn:hover {
  background: #4A90E2;
  color: white;
}

.quantity {
  font-size: 12px;
  font-weight: 500;
  color: #333;
  min-width: 16px;
  text-align: center;
}

.delete-button {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 50%;
  transition: background-color 0.2s ease;
}

.delete-button:hover {
  background: rgba(255, 107, 107, 0.1);
}

.delete-icon {
  font-size: 16px;
  color: #666;
}

.delete-button:hover .delete-icon {
  color: #ff6b6b;
}

/* 医疗实验室凭证 */
.certificate-section {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.certificate-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.certificate-icon {
  font-size: 16px;
}

.certificate-text {
  font-size: 14px;
  color: #333;
  flex: 1;
}

.certificate-name {
  font-size: 14px;
  color: #4A90E2;
  font-weight: 500;
}

/* 底部结算 */
.bottom-section {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  border-top: 1px solid #e0e0e0;
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1000;
}

.total-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.total-label {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.total-price {
  font-size: 20px;
  font-weight: 600;
  color: #4CAF50;
}

.submit-order-btn {
  background: #4A90E2;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.submit-order-btn:hover {
  background: #357ABD;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .header {
    padding: 12px 16px;
  }
  
  .back-arrow {
    width: 28px;
    height: 28px;
    font-size: 20px;
  }
  
  .title {
    font-size: 16px;
  }
  
  .address-section {
    padding: 16px;
  }
  
  .products-section {
    padding: 0 16px;
  }
  
  .product-item {
    padding: 12px 0;
  }
  
  .product-image {
    width: 50px;
    height: 50px;
  }
  
  .product-name {
    font-size: 13px;
  }
  
  .product-spec {
    font-size: 11px;
  }
  
  .price {
    font-size: 14px;
  }
  
  .quantity-control {
    padding: 2px 6px;
  }
  
  .quantity-btn {
    width: 20px;
    height: 20px;
    font-size: 14px;
  }
  
  .quantity {
    font-size: 12px;
  }
  
  .certificate-section {
    padding: 16px;
  }
  
  .bottom-section {
    padding: 12px 16px;
  }
  
  .total-label {
    font-size: 14px;
  }
  
  .total-price {
    font-size: 18px;
  }
  
  .submit-order-btn {
    padding: 10px 20px;
    font-size: 14px;
  }
}
</style> 